package domain;

import java.util.HashSet;
import java.util.Set;

import domain.interfaces.IdObject;

public class Contact implements IdObject {
	private String firstName;
	private String lastName;
	private String email;
	private long id;
	private Set<PhoneNumber> phones = new HashSet<PhoneNumber>();
	private Set<ContactGroup> books = new HashSet<ContactGroup>();
	private Address add;
	private int version = 1;

	public Contact(Contact c) {
		this.setFirstName(c.getFirstName());
		this.setLastName(c.getLastName());
		this.setEmail(c.getEmail());
		this.setId(c.getId());
		this.setPhones(c.getPhones());
		this.setBooks(c.getBooks());
		this.setAdd(c.getAdd());
	}

	public Contact(Address add, Set<ContactGroup> books, String email,
			String firstName, String lastName, Set<PhoneNumber> phones) {
		this.add = add;
		this.books = books;
		this.email = email;
		this.firstName = firstName;
		this.lastName = lastName;
		this.phones = phones;
	}

	public Contact() {

	}

	public Set<ContactGroup> getBooks() {
		return books;
	}

	public void setBooks(Set<ContactGroup> books) {
		this.books = books;
	}

	public void addBooks(ContactGroup cg) {
		if (books.contains(cg)) {
			return;
		}
		this.books.add(cg);
		cg.addContact(this);
	}

	public void removeBooks(ContactGroup cg) {
		if (!books.contains(cg))
			return;
		// remove the follower
		this.books.remove(cg);
		// remove myself from the follower
		cg.removeContact(this);
	}

	public void setVersion(int version) {
		this.version = version;
	}

	public int getVersion() {
		return version;
	}

	public Set<PhoneNumber> getPhones() {
		return new HashSet<PhoneNumber>(phones);
	}

	public void setPhones(Set<PhoneNumber> phones) {
		this.phones = phones;
	}

	public void addPhone(PhoneNumber p) {
		if (phones.contains(p)) {
			return;
		}
		this.phones.add(p);
		p.setContact(this);
	}

	public void removePhone(PhoneNumber p) {
		if (!phones.contains(p)) {
			return;
		}
		this.phones.remove(p);
		p.setContact(null);
	}

	public Address getAdd() {
		return this.add;
	}

	public void setAdd(Address add) {
		if (sameAsFormer(add)) {
			return;
		}
		this.add = add;
	}

	private boolean sameAsFormer(Address newAddress) {
		return add == null ? newAddress == null : add.equals(newAddress);
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String fname) {
		this.firstName = fname;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lname) {
		this.lastName = lname;
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String em) {
		this.email = em;
	}

	public long getId() {
		return this.id;
	}

	public void setId(long id) {
		this.id = id;
	}

}
